package logic

import (
	"context"
	"log"
	"shopping/models/mysql"

	"shopping/shoppingrpc/internal/svc"
	"shopping/shoppingrpc/shoppingrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductsAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProductsAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductsAddLogic {
	return &ProductsAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ProductsAddLogic) ProductsAdd(in *shoppingrpc.ProductsAddRequest) (*shoppingrpc.ProductsAddResponse, error) {
	// todo: add your logic here and delete this line

	product := mysql.Product{
		Name:        in.Name,
		Price:       float64(in.Price),
		Img:         in.Img,
		Description: in.Description,
		UserId:      in.UserId,
	}
	err := mysql.Db.Debug().Create(&product).Error
	if err != nil {
		return nil, err
	}
	log.Println(product.UserId, "发布商品", product.Name)
	return &shoppingrpc.ProductsAddResponse{Id: int64(product.ID)}, nil
}
